package team.ttorder.ttorder.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import team.ttorder.ttorder.dao.FoodMapper;
import team.ttorder.ttorder.dao.FoodTypeMapper;
import team.ttorder.ttorder.javabean.PageBean;
import team.ttorder.ttorder.model.Food;
import team.ttorder.ttorder.model.FoodType;
import team.ttorder.ttorder.utils.ImageUtils;

@Service
public class FoodServiceImpl implements IFoodService {

	@Autowired
	private FoodMapper foodMapper;

	@Autowired
	private FoodTypeMapper foodTypeMapper;

	/**
	 * 查找所有的菜,将菜品的菜系查找出来后，放到菜品类Food中，并且封装到PageBean类中
	 */
	@Override
	public PageBean<Food> getAllFood(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Food> foods = foodMapper.selectAll();
		try {
			FoodType foodType = null;
			for (int i = 0;i < foods.size();i++) {
				foodType = new FoodType();
				foodType = foodTypeMapper.selectByPrimaryKey(foods.get(i).getFoodTypeId());
				foods.get(i).setFoodTypeName(foodType.getTypeName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new PageBean<>(foods);
	}

	/**
	 * 更新菜色
	 */
	@Override
	public void updateFood(Food food) {
		try {
			foodMapper.updateByPrimaryKey(food);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加菜色
	 */
	@Override
	public void addFood(Food food) {
		foodMapper.insert(food);
	}

	/**
	 * 删除菜色
	 */
	@Override
	public void deleteFoodById(Integer id) {
		Food food = getFoodById(id);
		if(food.getImage() != null || food.getImage() != "") {
			ImageUtils.delete(this.getClass().getClassLoader().getResource("").getPath(), food.getImage());
		}
		foodMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<FoodType> getAllFoodType() {
		return foodTypeMapper.getAllFoodType();
	}

	@Override
	public Food getFoodById(Integer id) {
		return foodMapper.selectByPrimaryKey(id);
	}

	@Override
	public FoodType getFoodTypeById(Integer id) {
		return foodTypeMapper.selectByPrimaryKey(id);
	}

	@Override
	public String addFoodType(FoodType foodType) {
		FoodType ft = foodTypeMapper.selectByFoodTypeName(foodType.getTypeName());
		if (ft != null) {
			foodTypeMapper.insert(foodType);
			return "success";
		} else {
			return "fail";
		}
	}

	@Override
	public void deleteFoodTypeById(Integer id) {
		foodMapper.deleteByFoodTypeId(id);
		foodTypeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public String updateFoodType(FoodType foodType) {
		FoodType ft = foodTypeMapper.selectByFoodTypeName(foodType.getTypeName());
		if (ft != null) {
			foodTypeMapper.updateByPrimaryKey(foodType);
			return "success";
		} else {
			return "fail";
		}
		
	}

	@Override
	public List<Food> getAllFoods() {
		List<Food> foods = foodMapper.selectAll();
		try {
			FoodType foodType = null;
			for (int i = 0;i < foods.size();i++) {
				foodType = new FoodType();
				foodType = foodTypeMapper.selectByPrimaryKey(foods.get(i).getFoodTypeId());
				foods.get(i).setFoodTypeName(foodType.getTypeName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return foods;
	}

}
